package cn.juque.datapipeline.helper;

import cn.hutool.core.lang.Assert;
import cn.juque.datapipeline.config.PropertiesConfig;
import cn.juque.datapipeline.entity.TaskGroupInfo;
import cn.juque.datapipeline.mapper.TaskGroupInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>TaskGroupHelper</li>
 * </ul>
 * @date 2023-04-06 19:31:08
 **/
@Component("taskGroupHelper")
public class TaskGroupHelper {

    @Resource
    private PropertiesConfig propertiesConfig;

    @Resource
    private TaskGroupInfoMapper taskGroupInfoMapper;

    public Boolean updateStatus(String groupId, String batchNo, Integer oldStatus, Integer newStatus) {
        Assert.notEmpty(groupId);
        Assert.notEmpty(batchNo);
        Assert.notNull(oldStatus);
        Assert.notNull(newStatus);
        TaskGroupInfo taskGroupInfo = new TaskGroupInfo();
        taskGroupInfo.setExecuteBatchNo(batchNo);
        taskGroupInfo.setExecuteStatus(newStatus);
        taskGroupInfo.setExecuteIp(this.propertiesConfig.getBindIp());
        taskGroupInfo.setLastExecuteTime(new Date());
        TaskGroupInfo where = new TaskGroupInfo();
        where.setId(groupId);
        where.setExecuteStatus(oldStatus);
        return SqlHelper.retBool(this.taskGroupInfoMapper.update(taskGroupInfo, new QueryWrapper<>(where)));
    }

    public Boolean updateStatus(String groupId, Integer status) {
        Assert.notEmpty(groupId);
        Assert.notNull(status);
        TaskGroupInfo taskGroupInfo = new TaskGroupInfo();
        taskGroupInfo.setId(groupId);
        taskGroupInfo.setExecuteStatus(status);
        return SqlHelper.retBool(this.taskGroupInfoMapper.updateById(taskGroupInfo));
    }
}
